package com.rocksea.app.http;

import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.rocksea.app.entry.CheckVersion;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * Created by Mrw on 2018/1/19.
 * APP 升级Http请求
 *
 *
 */

public class UpDateTask extends AsyncTask<Void,Void,CheckVersion.DataBean.ListBean>
{
    private UpDateTask(CheckVersionCallBack checkVersionCallBack)
    {
        this.mCheckVersionCallBack = checkVersionCallBack;
    }
    @Override
    protected CheckVersion.DataBean.ListBean doInBackground(Void... voids)
    {
        String path = "http://www.r93535.com/gateway/inter/inter!getAppInfox.action";
        try
        {
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10*10000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            //String request = "{\"appId\":85}";
            String request = "appId=85";
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            printWriter.write(request);
            printWriter.flush();
            int code = httpURLConnection.getResponseCode();
            if (code==200)
            {
                String s= changeInputStream(httpURLConnection.getInputStream());
                System.out.println(s);
                CheckVersion checkVersion = JSON.parseObject(s, CheckVersion.class);
                if (checkVersion.getStatus().equals("0"))
                {
                    if (checkVersion.getData().getList()!=null&&checkVersion.getData().getList().size()>0)
                    {
                        return checkVersion.getData().getList().get(0);
                    }
                }
            }
            printWriter.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(CheckVersion.DataBean.ListBean checkInfo)
    {
        super.onPostExecute(checkInfo);
        if (mCheckVersionCallBack!=null)
            mCheckVersionCallBack.onCheckVersionCallBack(checkInfo);
    }
    private  String changeInputStream(InputStream inputStream) throws IOException
    {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte[] data = new byte[1024];
        int len = inputStream.read(data);
        while (len != -1)
        {
            outputStream.write(data, 0, len);
            len = inputStream.read(data);
        }
        return new String(outputStream.toByteArray());
    }
    public static void checkUpDate(CheckVersionCallBack checkVersionCallBack)
    {
        UpDateTask upDateTask = new UpDateTask(checkVersionCallBack);
        upDateTask.execute();
    }
    private CheckVersionCallBack mCheckVersionCallBack;
    public interface CheckVersionCallBack
    {
        void onCheckVersionCallBack(CheckVersion.DataBean.ListBean verInfo);
    }

}
